package com.xgd.phylw.controller;

import com.xgd.phylw.bean.Hall;
import com.xgd.phylw.bean.Movie;
import com.xgd.phylw.bean.Schedule;
import com.xgd.phylw.dto.Hall_ScheduleDto;
import com.xgd.phylw.dto.Movie_tempDto;
import com.xgd.phylw.service.BackPageService;
import com.xgd.phylw.service.HallService;
import com.xgd.phylw.service.MovieService;
import com.xgd.phylw.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.List;

@Controller
public class ScheduleController {
    @Autowired
    private MovieService movieService;
    @Autowired
    private ScheduleService scheduleService;
    @Autowired
    private HallService hallService;
    @Autowired
    private BackPageService backPageService;

    @RequestMapping(value = "/getMovieByMovieName",method = RequestMethod.GET)
    public String getMovieByMovieName(Movie movie, Model model){
        if (movie.getMovieName() != null &movie.getMovieName()!="") {
            Movie byName = movieService.MovieByName(movie);
            List<Hall_ScheduleDto> hall_scheduleDtos = scheduleService.selectSchedule(byName.getMovieId());
            model.addAttribute("movie",byName);
            model.addAttribute("hall",hall_scheduleDtos);
            return "cinema-movie";
        }
        return null;
    }

    @RequestMapping("/getMovie")
    public String getMovie(Movie movie,Model model){
        if (movie.getMovieName() != null &movie.getMovieName()!="") {
            List<Movie_tempDto> movieList = movieService.SelectAllMovie(new Movie());
            Movie byName = movieService.MovieByName(movie);
            List<Hall_ScheduleDto> hall_scheduleDtos = scheduleService.selectSchedule(byName.getMovieId());
            model.addAttribute("movie",byName);
            model.addAttribute("hall",hall_scheduleDtos);
            model.addAttribute("movieList",movieList);
            return "movie";
        }
        return null;
    }

    //团购 页面跳转以及数据显示
    @RequestMapping("MovieTui")
    public String PageChangeToMovieTui(Model model){
        model.addAttribute("movieTui","movieTui");
        return "movie";
    }
    //影院信息 页面跳转以及数据显示
    @RequestMapping("MovieXx")
    public String PageChangeToMovieXx(Model model){
        model.addAttribute("movieXx","movieXx");
        return "movie";
    }
    //后台 影厅模糊查询
    @RequestMapping("searchStudio")
    public String searchStudio(String stitle,Model model){
        List<Hall> halls =hallService.ScheduleAll(stitle) ;
        model.addAttribute("halls",halls);
        return "studio";
    }
    @RequestMapping("Studio")
    public String PageChangeToStudio(Model model){
        List<Hall> halls = hallService.SelectAllHall();
        model.addAttribute("halls",halls);
        return "studio";
    }

    @RequestMapping("deleteSchedule")
    public String DeleteSchedule(Integer scheduleId,Model model){
        Schedule schedule = new Schedule();
        schedule.setScheduleId(scheduleId);
        schedule.setMoviePrise((double) 0);
        scheduleService.deleteScheduleByPrimaryKey(schedule);
        List<Schedule> schedules = backPageService.SelectAllSchedule();
        model.addAttribute("schedules",schedules);
        return "admin_seat";
    }

    @RequestMapping("addSchedule")
    public String AddSchedule(Integer hallId, Integer movieId, Double prise, Date date,Integer hour,Integer mm,Model model){
        Schedule schedule = new Schedule();
        schedule.setHallId(hallId);
        schedule.setMovieId(movieId);
        schedule.setMoviePrise(prise);
        schedule.setBeginDate(date);
        schedule.setBeginHour(hour);
        schedule.setBeginMinute(mm);
        scheduleService.insert(schedule);
        List<Schedule> schedules = backPageService.SelectAllSchedule();
        model.addAttribute("schedules",schedules);
        return "admin_seat";
    }
}
